#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    int ListNode_len(ListNode* head)
    {
        int len = 0;
        ListNode* cur = head;
        while (cur)
        {
            len++;
            cur = cur->next;
        }
        return len;
    }
    ListNode* removeNthFromEnd(ListNode* head, int n)
    {
        ListNode* new_head = new ListNode(0, head);
        int len = ListNode_len(head);
        ListNode* cur = new_head;
        for (int i = 0; i < len - n; i++)
        {
            cur = cur->next;
        }
        ListNode* de = cur->next;
        cur->next = cur->next->next;
        delete de;
        return new_head->next;


    }
};